from django.http import HttpResponse
from django.utils.deprecation import MiddlewareMixin


# Django提供的django.utils.deprecation.MiddlewareMixin类，它能够轻松兼容新版的MIDDLEWARE和旧版的MIDDLEWARE_CLASSES。
class BlockedIPSMiddleware(MiddlewareMixin):
    """
    def __init__(self, get_response):
        print('------------__init__')
        self.get_response = get_response

    def __call__(self, request):
        print('------------__call__')
        return self.get_response(request)
    """

    """中间件类"""
    EXCLUDE_IPS = ['192.168.1.119']

    def process_request(self, request):
        print('BlockedIPSMiddleware------------process_request')

    def process_view(self, request, view_func, *view_args, **view_kwargs):
        """视图函数调用之前会调用"""
        print('BlockedIPSMiddleware------------process_view')
        user_ip = request.META['REMOTE_ADDR']
        if user_ip in BlockedIPSMiddleware.EXCLUDE_IPS:
            print('119被拦截了！')
            return HttpResponse('<h1>Forbidden</h1>')

    def process_response(self, request, response):
        print('BlockedIPSMiddleware------------process_response')
        return response


"""
from django.apps import AppConfig


class my_mid(AppConfig):

    def __init__(self):
        super(my_mid, self).__init__()
        print('------------init')

    def process_request(self, request):
        print('------------process_request')

    def process_view(self, request, view_func, *view_args, **view_kwargs):
        print('------------process_view')

    def process_response(self, request, response):
        print('------------process_response')
        return response
"""


class TestMiddleware(object):
    """中间件类"""

    def __init__(self, get_response):
        print('TestMiddleware------------__init__')
        self.get_response = get_response

    def __call__(self, request):
        print('TestMiddleware------------__call__')
        response = None
        if hasattr(self, 'process_request'):
            response = self.process_request(request)
        if not response:
            response = self.get_response(request)
        if hasattr(self, 'process_response'):
            response = self.process_response(request, response)
        return response

    def process_request(self, request):
        print('TestMiddleware------------process_request')

    def process_view(self, request, view_func, *view_args, **view_kwargs):
        print('TestMiddleware------------process_view')

    def process_response(self, request, response):
        print('TestMiddleware------------process_response')
        return response


class ExceptionTest1Middleware(object):

    def __init__(self, get_response):
        print('ExceptionTest1Middleware------------__init__')
        self.get_response = get_response

    def __call__(self, request):
        print('ExceptionTest1Middleware------------__call__')
        return self.get_response(request)

    def process_exception(self, request, exception):
        """视图函数发生异常时调用"""
        print('ExceptionTest1Middleware------------process_exception')


class ExceptionTest2Middleware(object):

    def __init__(self, get_response):
        print('ExceptionTest2Middleware------------__init__')
        self.get_response = get_response

    def __call__(self, request):
        print('ExceptionTest2Middleware------------__call__')
        return self.get_response(request)

    def process_exception(self, request, exception):
        """视图函数发生异常时调用"""
        print('ExceptionTest2Middleware------------process_exception')
